package cd.safe.school.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import cd.safe.school.R;
import cd.safe.school.bean.MusicCache;

public class MusicAdapter extends ArrayAdapter<MusicCache> {
	private Context context;
	public static List<MusicCache> list;
	private LayoutInflater in;

	public MusicAdapter(Context context, List<MusicCache> list) {
		super(context, R.layout.music_item, list);
		this.context = context;
		this.list = list;
		in = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public MusicCache getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = in.inflate(R.layout.music_item, null);
		}
		TextView music_name = (TextView) convertView
				.findViewById(R.id.music_name);
		RadioButton music_select = (RadioButton) convertView
				.findViewById(R.id.music_select);
		music_name.setText(list.get(position).getName());

		music_select.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					for (MusicCache m : list) {
						m.setSelect(false);
					}
				}
				list.get(position).setSelect(isChecked);
				notifyDataSetChanged();

			}
		});
		music_select.setChecked(list.get(position).isSelect());
		return convertView;
	}

}
